//
//  BaseTableViewController.swift
//  MedicalBeauty
//
//  Created by Henry on 2018/3/23.
//  Copyright © 2018年 shen_h. All rights reserved.
//

import UIKit
import MJRefresh

class BaseTableViewController: UITableViewController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        
        self.view.backgroundColor = UIColor.backgroundColor
        self.tableView.backgroundColor = UIColor.backgroundColor
        
        self.navigationController?.navigationBar.barTintColor = UIColor.init(hexColor: "#ffffff")
        self.navigationController?.navigationBar.isTranslucent = false
        self.navigationController?.navigationBar.tintColor = UIColor.black
//        self.navigationController?.navigationBar.shadowImage = UIImage()
        
        if self.navigationController?.viewControllers.count != 1 {
            self.setLeftBarButton(title: "", imageName: "icon_return", action: #selector(backBarButtonMethon))
        }
        
        if #available(iOS 11, *) {
            self.tableView.contentInsetAdjustmentBehavior = .never
        }else{
            self.automaticallyAdjustsScrollViewInsets = false
        }
        self.tableView.separatorColor = UIColor.viewLineColor
    }
    
    deinit {
        print("\(NSStringFromClass(self.classForCoder))  deinit")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.navigationController?.interactivePopGestureRecognizer?.delegate = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 0
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return 0
    }

}

extension BaseTableViewController{
    @objc func setLeftBarButton(title:String?,imageName:String, action:Selector){
        if imageName.isEmpty {
            
            let item = UIBarButtonItem(title: title, style: .plain, target: self, action: action)
            item.tintColor = UIColor.white
            self.navigationItem.leftBarButtonItem = item
            
        }else {
            let item = UIBarButtonItem(image: UIImage(named:imageName), style: .plain, target: self, action: action)
            item.title = ""
            self.navigationItem.leftBarButtonItem = item
        }
    }
    
    @objc func setRightBarButton(title:String,imageName:String, action:Selector,titleColor:String){
        if imageName.isEmpty {
            let item = UIBarButtonItem(title: title, style: .plain, target: self, action: action)
            item.tintColor = UIColor(hexColor: titleColor)
            item.setTitleTextAttributes([NSAttributedStringKey.font : UIFont.systemFont(ofSize: 14)], for: .normal)
            
            self.navigationItem.rightBarButtonItem = item
        }else {
            let item = UIBarButtonItem(image: UIImage(named:imageName), style: .plain, target: self, action: action)
            self.navigationItem.rightBarButtonItem = item
        }
    }
    
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.navigationController!.viewControllers.count <= Int(1) {
            return false
        }
        return true
    }
    
    @objc func backBarButtonMethon(){
        PKhud.hiden()
        self.navigationController?.popViewController(animated: true)
    }
    
    func pushWith(viewController ctrl:UIViewController){
        self.navigationController?.pushViewController(ctrl, animated: true)
    }
    
}























